var query_history = new Array();
var query_history_position = 0;

function resize_toolbar(size)
{
	toolbar_width = size['width'] - $('toolbar').getStripStyle('left') - $('toolbar').getStripStyle('margin-left') - $('toolbar').getStripStyle('margin-right') - $('toolbar').getStripStyle('padding-left') - $('toolbar').getStripStyle('padding-right');

	$('toolbar').setStyle({'width': (toolbar_width)+'px'});	
}


function run()
{
	var url = '../app/result.php';
	
	query_str = $('query_area').value

	add_history(query_str);
	
	if(query_str.substring(0,13).toLowerCase() == "select * from")
	{
		table_name = query_str.substring(13, query_str.length).toLowerCase().trim();
//		alert(table_name);
		if(table_name.indexOf(' ') < 0)
			ResultToolbar.editable(true);
		else
			ResultToolbar.editable(false);
	}
	else
	{
		ResultToolbar.editable(false);
	}
	
	
	new Ajax.Request(url, {
	  method: 'post',
	  postBody: 'action=query&query=' + escape(query_str) + '&schema=' + cw_schema,
	  onSuccess: function(transport) {
		  
		  var temp = transport.responseText;
		  $('result_area').update(temp);			 
	  }
	});		
}

function execute(str)
{
		var url = '../app/result.php';
	new Ajax.Request(url, {
	  method: 'post',
	  postBody: 'action=query&query=' + escape(str) + '&schema=' + cw_schema,
	  onSuccess: function(transport) {
			if(transport.responseText.substr(0,5) == 'error')
				alert(transport.responseText.substr(6,transport.responseText.length )); 
	  }
	});			
}


function add_history(str)
{
	query_history_position = query_history.length;
	query_history[query_history_position] = str;
	if(query_history_position > 0)
		$('back').removeClassName('disabled');	
}

function history_back()
{
	query_history_position--;
	$('query_area').value = query_history[query_history_position];
	if(query_history_position == 0)
		$('back').addClassName('disabled');
	if(query_history.length > query_history_position)
		$('forward').removeClassName('disabled');
}

function history_forward()
{
	query_history_position++;
	$('query_area').value = query_history[query_history_position];
	if(query_history.length-1 == query_history_position) 
		$('forward').addClassName('disabled');
	if(query_history_position > 0)
		$('back').removeClassName('disabled');			
}